package Dao.Impl;

import Dao.ProductDao;
import Domain.Product;
import Domain.Product2;
import JDBCUtils.JDBCUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class ProductDapImpl implements ProductDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public List<Product> FindProductById(int id) {
        String sql = "select\n" +
                " t.id,t.name,t.price,t.picture,t.num,t.descriptions,t.upTime,t.typeid,t.detail,t1.pname\n" +
                "from\n" +
                " product t\n" +
                "join\n" +
                " main_type t1\n" +
                "on\n" +
                " t.typeid = t1.id\n" +
                "where\n" +
                " t.id=?";
        List<Product> result = null;
        try {
            result = template.query(sql, new BeanPropertyRowMapper<>(Product.class), id);
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public List<Product> FindProductByTypeid(int tid) {
        String sql = "select * from product where typeid=?";
        List<Product> result = template.query(sql, new BeanPropertyRowMapper<>(Product.class), tid);
        return result;
    }

    @Override
    public List<Product> FindProductAll() {
        String sql = "select * from product";
        List<Product> all = template.query(sql, new BeanPropertyRowMapper<>(Product.class));
        return all;
    }

    @Override
    public List<Product> likeProduct(String Name) {
        String sql = "select * from product where name like '%' ? '%'";
        List<Product> products = template.query(sql, new BeanPropertyRowMapper<Product>(Product.class), Name);
        return  products;
    }



}
